package com.ruoyi.flowable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.domain.bo.BpmnConfigBo;
import com.ruoyi.flowable.domain.vo.BpmnConfigVo;
import com.ruoyi.flowable.service.IBpmnConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 节点配置
 *
 * @author zxw
 * @date 2023-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/flowable/bpmnConfig")
public class BpmnConfigController extends BaseController {

    private final IBpmnConfigService iBpmnConfigService;

    /**
     * 查询节点配置列表
     */
    @SaCheckPermission("flowable:bpmnConfig:list")
    @GetMapping("/list")
    public TableDataInfo<BpmnConfigVo> list(BpmnConfigBo bo, PageQuery pageQuery) {
        return iBpmnConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出节点配置列表
     */
    @SaCheckPermission("flowable:bpmnConfig:export")
    @Log(title = "节点配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BpmnConfigBo bo, HttpServletResponse response) {
        List<BpmnConfigVo> list = iBpmnConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "节点配置", BpmnConfigVo.class, response);
    }

    /**
     * 获取节点配置详细信息
     *
     * @param configId 主键
     */
    @SaCheckPermission("flowable:bpmnConfig:query")
    @GetMapping("/{configId}")
    public R<BpmnConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long configId) {
        return R.ok(iBpmnConfigService.queryById(configId));
    }

    /**
     * 获取节点配置详细信息
     *
     * @param appVersionId 版本id
     */
    @SaCheckPermission("flowable:bpmnConfig:query")
    @GetMapping("/appv/{appVersionId}")
    public R<BpmnConfigVo> getInfoByAppVersionId(@NotNull(message = "主键不能为空")
                                   @PathVariable Long appVersionId) {
        return R.ok(iBpmnConfigService.queryByAppVersionId(appVersionId));
    }

    /**
     * 新增节点配置
     */
    @SaCheckPermission("flowable:bpmnConfig:add")
    @Log(title = "节点配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BpmnConfigBo bo) {
        return toAjax(iBpmnConfigService.insertByBo(bo));
    }

    /**
     * 修改节点配置
     */
    @SaCheckPermission("flowable:bpmnConfig:edit")
    @Log(title = "节点配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BpmnConfigBo bo) {
        return toAjax(iBpmnConfigService.updateByBo(bo));
    }

    /**
     * 删除节点配置
     *
     * @param configIds 主键串
     */
    @SaCheckPermission("flowable:bpmnConfig:remove")
    @Log(title = "节点配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] configIds) {
        return toAjax(iBpmnConfigService.deleteWithValidByIds(Arrays.asList(configIds), true));
    }
}
